package medium

import (
	. "GoLeetcode/common"
	"math"
)

/*
时间复杂度:O(N)
空间复杂度:O(1)
*/

func minSubArrayLen(target int, nums []int) int {
	left, right := 0, 0
	sum := 0
	ans := math.MaxInt32
	for right < len(nums) {
		sum += nums[right]
		for sum >= target {
			ans = Min(ans, right-left+1)
			sum -= nums[left]
			left++
		}
		right++
	}
	if ans == math.MaxInt32 {
		return 0
	}
	return ans
}
